package com.atguigu.mr.wc;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.BZip2Codec;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * @description:
 * @create: 2019-12-15 13:16
 * @author: baojinlong
 **/
public class WordcountDriver {
    public static void main(String[] args) throws Exception {
        // 1.获取job对象
        Configuration conf = new Configuration();

        // 开启map端输出压缩
        conf.setBoolean("mapreduce.map.output.compress", true);
        // 设置map端输出压缩方式
        conf.setClass("mapreduce.map.output.compress.codec", BZip2Codec.class, CompressionCodec.class);

        Job job = Job.getInstance(conf);
        // 2.设置jar存储位置
        job.setJarByClass(WordcountDriver.class);
        // 3.关联mapper和reducer
        job.setMapperClass(WordcountMapper.class);
        job.setReducerClass(WordcountReduce.class);
        // 4.这是mapper输出的key和value类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //5.设置最终输出的key和value类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 设置reduce端输出压缩开启
        FileOutputFormat.setCompressOutput(job, true);
        // 设置压缩方式 也可以设置别的压缩方式不会报错
        FileOutputFormat.setOutputCompressorClass(job, BZip2Codec.class);

        // 5.1如果不设置InputFormat默认使用的是TextInputFormat.class,下面两个可以不加按照默认切片方式
        job.setInputFormatClass(CombineTextInputFormat.class);
        // 设置虚拟存储切片最大值4M normal:20M 或者har压缩
        CombineTextInputFormat.setMaxInputSplitSize(job, 4194304);

        // 6.设置输入和输出数据的路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        // 7.提交作业 不建议submit();
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
